//
//  WCBPayViewController.m
//  WeChatBuniess
//
//  Created by Guo Rui on 2017/12/19.
//  Copyright © 2017年 GR Harry. All rights reserved.
//

#import "WCBPayViewController.h"
#import "WCASessionManager.h"
#import "WCBDataHelper.h"
#import "UserModel.h"
#import "TFToast.h"
#import "WCBOrderInfoCell.h"
#import "WCBAddressCell.h"
#import "WCBPayQRcodeCell.h"
#import "TFWeakTimer.h"
#import "WCBPayResultViewController.h"

@interface WCBPayViewController ()

@property (strong, nonatomic) TFWeakTimer *timer;
@end

@implementation WCBPayViewController

- (instancetype)initWithStyle:(UITableViewStyle)style
{
    if (self = [super initWithStyle:UITableViewStyleGrouped]) {
        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"支付订单";
    self.view.backgroundColor = [UIColor colorWithRed:0.94 green:0.94 blue:0.95 alpha:1.00];
    
    [self.tableView registerClass:[WCBOrderInfoCell class] forCellReuseIdentifier:@"ID1"];
    [self.tableView registerClass:[WCBAddressCell class] forCellReuseIdentifier:@"ID2"];
    [self.tableView registerClass:[WCBPayQRcodeCell class] forCellReuseIdentifier:@"ID3"];
    
    self.tableView.tableFooterView = [[UIView alloc] init];
    
    [self polling];
}


#pragma mark - Private
- (void)polling
{
    self.timer = [TFWeakTimer scheduledTimerWithTimeInterval:1.5 target:self selector:@selector(requestForPayStatus) userInfo:nil repeats:YES dispatchQueue:dispatch_get_main_queue()];
}

- (void)requestForPayStatus
{
    if (self.payModel == nil || self.payModel.orderId.integerValue == 0) {
        return;
    }
    
    __weak WCBPayViewController *weakself = self;
    [WCASessionManager POST:@"weimport/mtime/getPayResult" parameters:@{ @"orderId" : self.payModel.orderId } success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"getPayResult - %@", responseObject);
    
        BOOL success = [[responseObject objectForKey:@"code"] isEqualToString:@"000"];
        if (success) {
            NSDictionary *reslut = responseObject[@"data"];
            if ([reslut[@"payResult"] integerValue] == 1) {
                [weakself.timer invalidate];
                weakself.timer = nil;
                
                dispatch_sync(dispatch_get_main_queue(), ^{
                    WCBPayResultViewController *vc = [[WCBPayResultViewController alloc] init];
                    [weakself.navigationController pushViewController:vc animated:YES];
                });
            }
        }
        else {
            dispatch_async(dispatch_get_main_queue(), ^{
                [TFToast toast:@"支付失败，请联系页面中的客服电话!"];
            });
        }
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        dispatch_async(dispatch_get_main_queue(), ^{
            NSLog(@"您的网络不给力，请检查网络后重试!");
        });
    }];
}



#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
       WCBOrderInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ID1" forIndexPath:indexPath];
        cell.bookModel = self.bookModel;
        return cell;
    }
    else if (indexPath.row == 1) {
        WCBAddressCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ID2" forIndexPath:indexPath];
        cell.addressModel = self.addressModel;
        return cell;
    }
    
    WCBPayQRcodeCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ID3" forIndexPath:indexPath];
    cell.payModel = self.payModel;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        return 235.f;
    }
    else if (indexPath.row == 1) {
        return 235.f;
    }
    return 510 + 20;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc] init];
    view.backgroundColor = [UIColor clearColor];
    view.frame = CGRectMake(0, 0, self.view.frame.size.width, 100);
    
    UILabel *label = [[UILabel alloc] init];
    label.textColor = [UIColor colorWithRed:0.60 green:0.60 blue:0.60 alpha:1.00];
    label.textAlignment = NSTextAlignmentCenter;
    label.font = [UIFont systemFontOfSize:20];
    label.numberOfLines = 0;
    label.text = @"亲爱的用户: 假如您在支付中遇到问题\n请联系客服电话: 400-102-1009";
    
    CGFloat x, y, w, h;
    x = 50.f;
    y = 20.f;
    w = CGRectGetWidth(self.view.frame) - 2 * x;
    h = 100 - y;
    label.frame = CGRectMake(x, y, w, h);
    [view addSubview:label];
    
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 100.f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return .1f;
}

- (void)dealloc {
    [self.timer invalidate];
    self.timer = nil;
    NSLog(@"\n************\n %@ dealloc \n************", self.class);
}

@end


@implementation WCBPayModel

@end
